//
//  B_ReleaseView.m
//  Application
//
//  Created by 薛小刚 on 2018/8/9.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "B_ReleaseView.h"
#import "B_ReleaseTopicVC.h"
#import "B_ReleaseActivityVC.h"


@interface B_ReleaseView()

@property (nonatomic, strong) UIButton * leftButton, * rightButton, * closeButton;
@property (nonatomic, strong) UIView * backView;
@property (nonatomic, assign) BOOL isBusiness;

@end

@implementation B_ReleaseView

- (instancetype)init {
    if (self = [super init]) {
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        self.backgroundColor = [UIColor clearColor];
        
        [self addSubview:self.backView];
        [self addSubview:self.leftButton];
        [self addSubview:self.rightButton];
        [self addSubview:self.closeButton];
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo([UIApplication sharedApplication].keyWindow.mas_top);
            make.left.equalTo([UIApplication sharedApplication].keyWindow.mas_left);
            make.right.equalTo([UIApplication sharedApplication].keyWindow.mas_right);
            make.bottom.equalTo([UIApplication sharedApplication].keyWindow.mas_bottom);
        }];
        [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top);
            make.left.equalTo(self.mas_left);
            make.right.equalTo(self.mas_right);
            make.bottom.equalTo(self.mas_bottom);
        }];
        
        [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(100));
            make.height.equalTo(@(100));
            make.left.equalTo(self.mas_left).offset(50);
            make.bottom.equalTo(self.mas_bottom);
        }];
        [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(100));
            make.height.equalTo(@(100));
            make.right.equalTo(self.mas_right).offset(-50);
            make.bottom.equalTo(self.mas_bottom);
        }];
    }
    return self;
}

- (void) show {
    
    
    [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(self.frame.size.width));
        make.height.equalTo(@(self.frame.size.height));
        make.centerX.equalTo(self.mas_centerX);
        make.bottom.equalTo(self.mas_bottom).offset(-(self.size.height > 800 ? 83 - self.frame.size.height - self.frame.origin.y : 49 - self.frame.size.height - self.frame.origin.y));
    }];
    
    [UIView animateWithDuration:0.2 animations:^{
        self.backView.alpha =1;
    }];
    
    [UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.leftButton.transform = CGAffineTransformMakeTranslation(0, -200);
    } completion:^(BOOL finished) {
    }];
    [UIView animateWithDuration:0.2 delay:0.1 usingSpringWithDamping:0.4 initialSpringVelocity:10 options:UIViewAnimationOptionCurveEaseIn animations:^{
        self.rightButton.transform = CGAffineTransformMakeTranslation(0, -200);
    } completion:^(BOOL finished) {
    }];
    
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor whiteColor];
        _backView.alpha = 0;
    }
    return _backView;
}

- (UIButton *)closeButton {
    if (!_closeButton) {
        _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_closeButton setImage:[UIImage imageNamed:@"B_release_bounced_close"] forState:UIControlStateNormal];
        [_closeButton addTarget:self action:@selector(closeButtonTapAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _closeButton;
}

- (UIButton *)leftButton {
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftButton.frame = CGRectMake(0, 0, 100, 100);
        [_leftButton setImage:[UIImage imageNamed:@"B_release_bounced_topic"] forState:UIControlStateNormal];
        [_leftButton setTitle:@"发布话题" forState:UIControlStateNormal];
        [_leftButton setTitleColor:[UIColor color_text_deep_gray] forState:UIControlStateNormal];
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_leftButton layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:14];
        [_leftButton addTarget:self action:@selector(leftButtonTapAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(0, 0, 100, 100);
        [_rightButton setImage:[UIImage imageNamed:@"B_release_bounced_activity"] forState:UIControlStateNormal];
        [_rightButton setTitle:@"发布活动" forState:UIControlStateNormal];
        [_rightButton setTitleColor:[UIColor color_text_deep_gray] forState:UIControlStateNormal];
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [_rightButton layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:14];
        [_rightButton addTarget:self action:@selector(rightButtonTapAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss];
}

- (void) closeButtonTapAction {
    [self dismiss];
}

- (void) leftButtonTapAction:(UIButton *) button {
    dispatch_async(dispatch_get_main_queue(), ^{
        B_ReleaseTopicVC * b = [[B_ReleaseTopicVC alloc] init];
        UINavigationController * nb = [[UINavigationController alloc] initWithRootViewController:b];
        [getNowVC() presentViewController:nb animated:YES completion:nil];
        [self dismiss];
    });
}

- (void)rightButtonTapAction:(UIButton *) button {
//    [self getData];
    B_ReleaseActivityVC * b = [[B_ReleaseActivityVC alloc] init];
    UINavigationController * nb = [[UINavigationController alloc] initWithRootViewController:b];
    [getNowVC() presentViewController:nb animated:YES completion:nil];
    [self removeFromSuperview];
}

- (void) dismiss {
    [UIView animateWithDuration:0.2 animations:^{
        self.backView.alpha = 0.5;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

/**
 TODO:获取用户的状态,如果不是商家是没有添加商品的
 */
- (void)getData {
    WeakSelf(weSelf, self);
    [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/user/selectUserInfo/%@", getUserId()] parameters:nil isCache:YES succeed:^(id data) {
        if ([data[@"code"] isEqualToString:@"20010"]) {
            if ([[NSString stringWithFormat:@"%@", data[@"values"][@"businessState"]] isEqualToString:@"2"]) {
                weSelf.isBusiness = YES;
                B_ReleaseActivityVC * b = [[B_ReleaseActivityVC alloc] init];
                UINavigationController * nb = [[UINavigationController alloc] initWithRootViewController:b];
                [getNowVC() presentViewController:nb animated:YES completion:nil];
                [weSelf removeFromSuperview];
            } else {
                weSelf.isBusiness = NO;
            }
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
    } fail:^(NSError *error) {
        [KeyWindow makeToast:@"服务器异常"];
    }];
}


@end
